import { NetService } from '../../common/NetService';
export default {
	data() {
		return {
			form: {
				openid: '',
				userName: '',
				phone: '',
				nickname: '',
				sex: '',
				headimgurl: '',
				country: '',
				unionid: '',
				city: '',
				province: ''
			},
			rules: {
				userName: {
					rule: /\S/,
					msg: '姓名不能为空'
				},
				phone: {
					rule: /^[1][0-9][0-9]{9}$/,
					msg: '请输入正确手机号'
				}
			},
			isDis: false,
			// 验证码输入框的值
			identifyingCodeVal: '',
			// 隐藏验证码输入框的清除
			// 获取动态码按钮的文字
			identifyingCodeBtnText: '获取动态码',
			// 动态码倒计时
			cutdown: 60,
			// 动态码倒计时
			timer: null,
			disabled: false,
		};
	},
	methods: {
		// 获取动态码
		getIdentifyingCode() {
			if(this.timer === null) {
				if(!this.validate('phone')) return;
				// 验证通过
				let data = {
					phone: this.form.phone,
				}
				NetService.sendVerifyCode(data).then(res => {
					/*   if (res) {
					     Util.showToast('短信发送成功');
					    
					   } else {
					     Util.showToast('短信发送失败');
					   }*/
					this.getIdentifyingCodeCutdown();
				}).catch(err => {
					console.error(err);
				});
			}
		},
		// 获取验证码倒计时
		getIdentifyingCodeCutdown() {
			if(this.timer === null) {
				let cutdown = this.cutdown;
				let timer = setInterval(() => {
					--cutdown;
					this.identifyingCodeBtnText = cutdown + 's';
					this.disabled = true;
					if(cutdown < 1) {
						clearInterval(timer);
						this.timer = null;
						this.disabled = false;
						this.identifyingCodeBtnText = '获取动态码';
					}
				}, 1000);
				this.timer = timer;
			}
		},
		validate(key) {
			let bool = true;
			if(!this.rules[key].rule.test(this.form[key])) {
				//提示信息
				uni.showToast({
					icon: "none",
					title: this.rules[key].msg
				});
				//取反
				bool = false;
				return false;
			}
			return bool;
		},
		inputChange(e) {
			let type = e.currentTarget.dataset.i;
			this.form[type] = e.detail.value;
		},
		inputChange2(e) {
			this.identifyingCodeVal = e.detail.value;
		},
		bindOk() {
			let _this = this;
			if(!this.validate('userName')) return;
			if(!this.validate('phone')) return;
			if(!this.identifyingCodeVal) {
				uni.showToast({
					icon: "none",
					title: "请输入验证码"
				});
				return false;
			}
			let data = JSON.parse(JSON.stringify(this.form));
			console.log('data', data);
			this.isDis = true;
			let data3 = {
				phone: this.form.phone,
				code: this.identifyingCodeVal,
			}
			NetService.codeVerify(data3)
				.then(res3 => {
					NetService.wxsaveuser(data)
						.then(res => {
							let oid = uni.getStorageSync('openId');
							NetService.wxgetuser(oid)
								.then(res2 => {
									uni.setStorageSync('user', res2);
									_this.isDis = false;
									uni.showToast({
										title: "绑定成功，为您返回上一页"
									});
									setTimeout(function() {
										uni.navigateTo({
											url: "/pages/index/index"
										});
									}, 1000);
								})
								.catch(err => {
									_this.isDis = false;
								});

						})
						.catch(err => {
							_this.isDis = false;
						});
				}).catch(err => {
					uni.showToast({
						icon: "none",
						title: "验证码错误"
					});
				})
		},
		init() {
			let id = uni.getStorageSync('openId');
			let u = uni.getStorageSync('user');
			for(let key in u) {
				if(this.form[key] != undefined) {
					this.form[key] = u[key];
				}
			}
			NetService.wxgetuser(id)
				.then(res => {
					for(let key in res) {
						console.log(key, this.form[key] != undefined)
						if(this.form[key] != undefined) {
							this.form[key] = res[key];
						}
					}
					console.log(this.form, 'this.form');
				})
				.catch(err => {});
		}
	},
	onLoad() {
		let _this = this;
		this.$getOpenId(() => {
			_this.init();
			//#ifdef H5
			// 判断是否是微信内
			let en = window.navigator.userAgent.toLowerCase();
			// 匹配en中是否含有MicroMessenger字符串
			if(en.match(/MicroMessenger/i) == 'micromessenger') {
				_this.$share({
					title: "用户绑定"
				});
			}
			//#endif
		});
	}
};